package com.yupi.yupao.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yupi.yupao.config.RedisConfig;
import com.yupi.yupao.model.domain.User;
import javafx.util.Pair;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: Peter
 * @Date: 2024-05-29-21:09
 * @Description: Pe
 */
public class AlgorithmUtils {
    @Resource
    private RedisConfig redisConfig;

    /**
     * 编辑距离算法（用于计算最相似的两组标签）
     * 原理：https://blog.csdn.net/DBC_121/article/details/104198838
     *
     * @param tagList1
     * @param tagList2
     * @return
     */
    public static int minDistance(List<String> tagList1, List<String> tagList2) {
        int n = tagList1.size();
        int m = tagList2.size();

        if (n * m == 0) {
            return n + m;
        }

        int[][] d = new int[n + 1][m + 1];
        for (int i = 0; i < n + 1; i++) {
            d[i][0] = i;
        }

        for (int j = 0; j < m + 1; j++) {
            d[0][j] = j;
        }

        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < m + 1; j++) {
                int left = d[i - 1][j] + 1;
                int down = d[i][j - 1] + 1;
                int left_down = d[i - 1][j - 1];
                if (!Objects.equals(tagList1.get(i - 1), tagList2.get(j - 1))) {
                    left_down += 1;
                }
                d[i][j] = Math.min(left, Math.min(down, left_down));
            }
        }
        return d[n][m];
    }

    /**
     *
     *
     *
     * @param str1
     * @param str2
     * @return
     */
    public static int minDistance(String str1, String str2) {
        int m = str1.length();
        int n = str2.length();

        // dp数组，dp[i][j]表示将str1的前i个字符变为str2的前j个字符的编辑距离
        int[][] dp = new int[m + 1][n + 1];

        // 初始化边界情况
        for (int i = 0; i <= m; i++) {
            dp[i][0] = i; // 删除操作
        }
        for (int j = 0; j <= n; j++) {
            dp[0][j] = j; // 插入操作
        }

        // 填充dp数组
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1]; // 相等，不需要操作
                } else {
                    dp[i][j] = Math.min(dp[i - 1][j], // 删除
                            Math.min(dp[i][j - 1], // 插入
                                    dp[i - 1][j - 1])) + 1; // 替换
                }
            }
        }

        // 返回编辑距离
        return dp[m][n];
    }

    // 新方法，用于匹配两个List<String>集合中的每个元素
    public static void matchLists(List<String> listStr1, List<String> listStr2) {
        List<Pair<User, Long>> list = new ArrayList<>();
        // 遍历listStr1中的每个元素
        for (int i = 0; i < listStr1.size(); i++) {
            String str1 = listStr1.get(i);

            // 遍历listStr2中的每个元素
            for (int j = 0; j < listStr2.size(); j++) {
                String str2 = listStr2.get(j);

                // 计算str1和str2之间的编辑距离
                int distance = minDistance(str1, str2);
                System.out.println("The edit distance between \"" + str1 + "\" and \"" + str2 + "\" is: " + distance);
            }
        }
    }
}
